package code.class2;


import common.ArrayUtils;

import java.util.Arrays;

public class TestDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 4, 5, 6, 2, 6, 2, 7, 2, 9, 5, 3};
        int k = 10;
        int kthSmalest = findKthSmalest(arr, k);
        System.out.println("kthSmalest = " + kthSmalest);
    }

    private static int findKthSmalest(int[] arr, int max) {
        int l = 0, sum = 0, count = 0;

        for (int r = 0; r < arr.length; r++) {
            sum += arr[r];

            // 收缩窗口直到 sum <= max
            while (sum > max && l <= r) {
                sum -= arr[l];
                l++;
            }
            ArrayUtils.printArr(Arrays.copyOfRange(arr, l, r+1));

            // 计算以 `r` 结尾的子数组个数
            count += (r - l + 1);
        }

        return count;
    }


}
